package cn.itcast.usermanage.web;

import cn.itcast.usermanage.bean.UserForm;
import cn.itcast.usermanage.pojo.User;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/prc")
public class ParamReceiverController {

    // UserForm类中属性包含User数组,可以成功接收数据
    @RequestMapping(value = "/userFormContainArray")
    @ResponseStatus(value = HttpStatus.OK)
    public void userFormContainArray(UserForm userForm, HttpServletResponse response) throws Exception {
        for (User user : userForm.getUsers()) {
            System.out.println(user);
        }

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("我来自ParamReceiverController类的userFormContainArray方法");
    }

    // 直接用User数组接收数据报错,因为没有 User[] 的默认无参构造
//	BeanInstantiationException: Failed to instantiate [[Lcn.itcast.usermanage.pojo.User;]: No default constructor found;
//	nested exception is java.lang.NoSuchMethodException
    @RequestMapping(value = "/userArrayDirect")
    @ResponseStatus(value = HttpStatus.OK)
    public void userArrayDirect(User[] users, HttpServletResponse response) throws Exception {
        System.out.println("进入ParamReceiverController类的test11方法,users.length=" + users.length);
        for (User user : users) {
            System.out.println(user);
        }

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("我来自ParamReceiverController类的userArrayDirect方法");
    }

    // http://127.0.0.1:8080/prc/listString?ids=1&ids=11
    @RequestMapping(value = "/listString")
    @ResponseStatus(value = HttpStatus.OK)
    public void listString(@RequestParam("ids") List<String> users, HttpServletResponse response) throws Exception {
        System.out.println("进入ParamReceiverController类的listString方法,users.size=" + users.size());
        for (String s : users
        ) {
            System.out.println(s);
        }

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("我来自ParamReceiverController类的listString方法");
    }

//   http://127.0.0.1:8080/prc/arrayListString?ids=1&ids=11
    @RequestMapping(value = "/arrayListString")
    @ResponseStatus(value = HttpStatus.OK)
    public void arrayListString(@RequestParam("ids") ArrayList<String> users, HttpServletResponse response) throws Exception {
        System.out.println("进入ParamReceiverController类的arrayListString方法,users.size=" + users.size());
        for (String s : users
        ) {
            System.out.println(s);
        }

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("我来自ParamReceiverController类的arrayListString方法");
    }

    // 用String数组可以成功接收数据
    // http://localhost:8080/prc/stringArray?users=11&users=22
    @RequestMapping(value = "/stringArray")
    @ResponseStatus(value = HttpStatus.OK)
    public void stringArray(String[] users, HttpServletResponse response) throws Exception {
        System.out.println("进入ParamReceiverController类的stringArray方法,users.length=" + users.length);
        for (String s : users
        ) {
            System.out.println(s);
        }

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("我来自ParamReceiverController类的stringArray方法");
    }

    // http://localhost:8080/prc/paraReceive?name=scy&age1=22&addr=shanghai
    // 加RequestParam后,必须以注解中value为准,不加注解,以形参为准
    //如果不加RequestParam注解,如果maven-compiler-plugin中配置 -g:none ,此时字节码没有LocalVariableTable,字节码中没有有方法参数名,则会报以下错误
    // java.lang.IllegalArgumentException: Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
    // 默认情况下,maven-compiler-plugin中默认配置为 -g,字节码会保留LocalVariableTable,字节码中有方法参数名
    @RequestMapping(value = "/paraReceive")
    @ResponseStatus(value = HttpStatus.OK)
    public void paraReceive(String name,
                            @RequestParam("addr") String addr,
                            @RequestParam(value = "age1", required = false) String age,
                            HttpServletResponse response
    ) throws Exception {

        System.out.println("名字=" + name + "----地址=" + addr + "----年龄=" + age);

        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("我来自ParamReceiverController类的paraReceive方法");
    }
//

    /*
      http://localhost:8080/prc/json2pojo

      {"id":3,"userName":null,"password":null,"name":"科比","age":null,"sex":null,"birthday":null,"created":null,"updated":null}
     */

    @RequestMapping(value = "/json2pojo")
//    @ResponseStatus(value = HttpStatus.OK) // 只要有返回即可,不需要特别指定HttpStatus
    public void json2pojo(@RequestBody User user, HttpServletResponse response) throws Exception {
        System.out.println(user);
        response.getWriter().print("我来自ParamReceiverController类的json2pojo方法");
    }


        /*
      http://localhost:8080/prc/json2pojo2

      {"id":3,"userName":null,"password":null,"name":"科比","age":null,"sex":null,"birthday":null,"created":null,"updated":null}

     */
        @RequestMapping(value = "/json2pojo2")
//    @PostMapping (value = "/json2pojo2")
//    @ResponseStatus(value = HttpStatus.OK) // 只要有返回即可,不需要特别指定HttpStatus
    @ResponseBody
    public User json2pojo2(@RequestBody User user) throws Exception {
        System.out.println(user);
        user.setBirthday(new Date());
        user.setCreated(new Date());
        return user;
    }

    /*
      http://localhost:8080/prc/json2listPojo

      [{"id":1,"userName":null,"password":null,"name":"戈尔丹","age":null,"sex":null,"birthday":null,"created":null,"updated":null},
      {"id":2,"userName":null,"password":null,"name":"伊利丹","age":null,"sex":null,"birthday":null,"created":null,"updated":null},
      {"id":3,"userName":null,"password":null,"name":"科比","age":null,"sex":null,"birthday":null,"created":null,"updated":null}]
     */
    @RequestMapping(value = "json2listPojo")
    @ResponseStatus(value = HttpStatus.OK) // 此时没有返回,所以必须要指定HttpStatus,不然页面报404错误
    public void json2listPojo(@RequestBody List<User> users) {
        for (User user : users) {
            System.out.println(user);
        }
    }


}
